package com.mall.common.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.domain.content.AdvertDomain;
import com.mall.common.domain.content.AdvertPositionDomain;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Updater;
import com.mall.common.service.AdvertPositionService;
import com.mall.common.service.AdvertService;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;
import com.mall.common.web.Servlets;


/**
 * 后台Action类 - 广告
 */
@Controller
@RequestMapping("/admin/system/advert")
public class AdvertController extends BaseController{

	@Resource private AdvertService advertService; 
	@Resource private AdvertPositionService advertPositionService; 

	/**
	 * 跳转到列表页面
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model){
		model.addAttribute("allAdvertPosition", advertPositionService.getAll());
		return "/admin/system/advert/list";
	}

	//获取列表
	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(Pager pager, HttpServletRequest request){
    	Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(filterParams);
    	pager = advertService.findPage(pager, filters);
		return JsonDataGridHelper.createJSONData(pager);
    }
    
	//编辑
	@Validations(
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "id", message = "广告不存在!")
		}
	)
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String edit(Long id, Model model){
		AdvertDomain advertDomain = advertService.find(id);
		AdvertPositionDomain position = advertDomain.getAdvertPosition();
		if(position == null){
			throw new NoticeException("广告不存在");
		}
    	model.addAttribute("advertDomain", advertDomain);
		model.addAttribute("allAdvertPosition", advertPositionService.getAll());
		return "/admin/system/advert/input";
	}
	
	// 修改
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "name", message = "广告名称不允许为空!"),
			@RequiredStringValidator(fieldName = "url", message = "广告链接不允许为空!")
		},
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "id", message = "广告不存在!"),
			@RequiredFieldValidator(fieldName = "advertPosition.id", message = "广告位置不允许为空!")
		}
	)
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(AdvertDomain advertDomain, HttpServletResponse response){
		AdvertPositionDomain position = advertPositionService.find(advertDomain.getAdvertPosition().getId());
		if(position == null){
			throw new NoticeException("广告位不存在");
		}
		Updater<AdvertDomain> updater = new Updater<AdvertDomain>(advertDomain);
		updater.include("startTime").include("endTime");
		advertService.updateByUpdater(updater);
		return ResponseUtils.renderJSON(successMessage, response);
	}
	
    /**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String input(Long positionId, Model model){
		if(positionId != null){
			AdvertPositionDomain position = advertPositionService.find(positionId);
			if(position != null){
				model.addAttribute("position", position);
			}
		}
		model.addAttribute("allAdvertPosition", advertPositionService.getAll());
		return "/admin/system/advert/input";
	}
	
	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "name", message = "广告名称不允许为空!"),
			@RequiredStringValidator(fieldName = "url", message = "广告链接不允许为空!")
		},
		requiredFields = { 
			@RequiredFieldValidator(fieldName = "advertPosition.id", message = "广告位置不允许为空!")
		}
	)
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public String save(AdvertDomain advertDomain, HttpServletResponse response){ 	
		AdvertPositionDomain position = advertPositionService.find(advertDomain.getAdvertPosition().getId());
		if(position == null){
			throw new NoticeException("广告位不存在");
		}
		advertDomain.setClickNumber(0);		
		advertService.persist(advertDomain);
		return ResponseUtils.renderJSON(successMessage, response);	
    }
	
    /**
	 * 删除广告
	 * @return
	 */
	@Validations(
		requiredFields = {
			@RequiredFieldValidator(fieldName = "ids", message = "广告不存在")	
		}
	)
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(Long[] ids, HttpServletResponse response){
    	advertService.remove(ids);
    	return ResponseUtils.renderJSON(successMessage, response);
    }

}
